package utils.linkedlist;

import sun.security.util.Length;

import java.util.Random;

/**
 * @author yaohuaiying
 * @create 2018-02-23 10:39
 **/
public class MyLink {
    private LinkNode head;
    private LinkNode rear;
    private int length;

    public MyLink(){
        head = new LinkNode();
        rear = head;
        length = 0;
    }

    /**
     * 添加
     * @param value
     */
    public void add(int value){
        rear.setNext(new LinkNode(value));
        rear = rear.getNext();
        length++;
    }

    /**
     * 添加
     * @param node
     */
    public void add(LinkNode node){
        rear.setNext(node);
        rear = node;
        length++;
    }

    /**
     * 打印
     */
    public void print(){
        LinkNode.nodePrint(head.getNext());
    }

    /**
     * 随机创建MyLink
     * @param length
     * @return
     */
    public static MyLink createMyLink(int length){
        Random random = new Random();
        MyLink myLink =new MyLink();
        for(int i=0; i<length; i++){
            myLink.add(random.nextInt(100));
        }
        return myLink;
    }

    /**
     * 翻转
     */
    public void overTurn(){
        LinkNode current = head.getNext();
        LinkNode temp = new LinkNode();
        while(current != null){
            LinkNode currentNext = current.getNext();
            current.setNext(temp);
            temp = current;
            current = currentNext;
        }
        head.setNext(temp);
    }

    public LinkNode getHead() {
        return head;
    }

    public void setHead(LinkNode head) {
        this.head = head;
    }

    public LinkNode getRear() {
        return rear;
    }

    public void setRear(LinkNode rear) {
        this.rear = rear;
    }

}
